package com.gx.smart.construction.ui.common.page;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.gx.smart.base.BaseAdapter;
import com.gx.smart.base.BaseMVVMActivity;
import com.gx.smart.base.BaseMVVMActivityV2;
import com.gx.smart.construction.R;
import com.gx.smart.construction.databinding.ActivitySearchRoomBinding;
import com.gx.smart.construction.ui.common.page.adapter.RoomListAdapter;
import com.gx.smart.construction.ui.common.page.viewmodel.SearchRoomViewModel;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureDto;

/**
 * 公共的房屋列表搜索模块-支持模糊查询
 */
public class SearchRoomActivity extends BaseMVVMActivityV2<SearchRoomViewModel>
        implements BaseAdapter.OnItemClickListener<ConstructionAppOrganizationalStructureDto> {

    @Override
    protected Class<SearchRoomViewModel> onBindViewModel() {
        return SearchRoomViewModel.class;
    }
    private RoomListAdapter adapter;
    private ActivitySearchRoomBinding binding;
    @Override
    protected int onBindLayout() {
        return R.layout.activity_search_room;
    }

    @Override
    public void initContentView(ViewGroup root) {
        binding = ActivitySearchRoomBinding.inflate(getLayoutInflater(), root, true);
        adapter = new RoomListAdapter(this);
        adapter.setItemClickListener(this);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        Drawable drawable = getResources().getDrawable(R.drawable.shape_list_item_divider_line);
        divider.setDrawable(drawable);
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setAdapter(adapter);
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        String parentId = getIntent().getStringExtra("parentId");
        viewModel.roomList(parentId);
    }

    @Override
    protected void initObserver() {
        viewModel.roomList.observe(this, list->{
            adapter.addAll(list);
        });
    }

    @Override
    public String getToolbarTitle() {
        return "搜索房间";
    }

    @Override
    public void onItemClick(ConstructionAppOrganizationalStructureDto item, int position) {
        //选中房间
        Intent intent = new Intent();
        intent.putExtra("number", item.getNumber());
        intent.putExtra("id", item.getId());
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}